<template>
  <div class="app-container">
    <el-form :inline="true" :model="order" class="demo-form-inline">
      <el-form-item label="id">
        <el-input v-model="order.id" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="订单类型">
        <el-select v-model="order.orderType" placeholder="请选择">
        <el-option
          v-for="item in this.$store.state.dict.orderType"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="order.orderStatus" placeholder="请选择">
        <el-option
          v-for="item in this.$store.state.dict.orderStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="积分抵扣">
        <el-select v-model="order.useIntegral" placeholder="请选择">
        <el-option
          v-for="item in this.$store.state.dict.useIntegral"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onClear">清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="danger" v-show="showDelBtn" @click="delOrders"
      >删除</el-button
    >
    <el-table
      v-loading="listLoading"
      :data="orders"
      element-loading-text="Loading"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <!--
      <el-table-column label="电子书封面" width="300" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height:100px"
            :src="imgPath+scope.row.orderImg"
            fit="fit"
          />
        </template>
      </el-table-column>
      -->
      <el-table-column label="订单类型" width="300" :formatter="TypeFormate" align="center">
      </el-table-column>

      <el-table-column label="商品" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.goodsId }}
        </template>
      </el-table-column>
      <!--
      <el-table-column label="电子书简介" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.bookInfo }}
        </template>
      </el-table-column>
      <el-table-column label="电子书描述" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.bookRemarks }}
        </template>
      </el-table-column>
      -->
      <el-table-column label="价格" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" :formatter="OrderStatusFormate" width="95" align="center">
      </el-table-column>
      <el-table-column label="上传人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>
            {{ scope.row.createTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="linkToEdit(scope.row.id)"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="delOrder(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <nav style="text-align: center">
      <el-pagination
        background
        @current-change="handlePageChange"
        :current-page="pageNo"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </nav>
  </div>
</template>

<script>
import { getOrdersApi, delOrderApi, delOrdersApi } from "@/api/order";
import { OrderStatusFormate, TypeFormate } from '@/utils/MyFormate'
export default {
  data() {
    return {
      order: {},
      pageNo: 1,
      pageSize: 10,
      orders: [],
      listLoading: true,
      delOrderArray: [],
      showDelBtn: false,
      total: 0,
    };
  },
  created() {
    this.getOrders(this.pageNo, this.pageSize, this.order);
  },
  methods: {
    OrderStatusFormate,
    TypeFormate,
    handlePageChange(currentPage) {
      this.pageNo = currentPage;
      this.getOrders(this.pageNo, this.pageSize, this.order);
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.showDelBtn = true;
      } else {
        this.showDelBtn = false;
      }
      this.delOrderArray = val;
    },
    delOrders() {
      this.listLoading = true;
      delOrdersApi(this.delOrderArray).then((response) => {
        this.getOrders(this.pageNo, this.pageSize);
        this.listLoading = false;
      });
    },
    onSearch() {
      this.pageNo = 1;
      this.getOrders(this.pageNo, this.pageSize, this.order);
    },
    onClear() {
      this.order = {};
      this.getOrders(this.pageNo, this.pageSize, this.order);
    },
    getOrders(pageNo, pageSize, order) {
      this.listLoading = true;
      getOrdersApi(pageNo, pageSize, this.order).then((response) => {
        this.orders = response.data.list;
        this.pageNo = response.data.pageNum;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    linkToEdit(id) {
      this.$router.push({ name: "editorder", params: { id: id } });
    },
    delOrder(id) {
      this.listLoading = true;
      delOrderApi(id).then((response) => {
        this.getOrders(this.pageNo, this.pageSize);
        this.listLoading = false;
      });
    },
  },
};
</script>
